package ui;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Ui {

	private javax.swing.JFrame frame;

	private ui.DiskUi ui1 = new ui.DiskUi();
	private ui.DevUi ui2=new ui.DevUi();
	private ui.StorageUi ui3=new ui.StorageUi();
	private ProcUi ui4=new ProcUi();
	
	public Ui() {
		frame = new javax.swing.JFrame();
		frame.setTitle("os");
		frame.setLayout(new GridLayout(1, 4));
		JButton devJbt = new JButton("设备管理");
		devJbt.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				ui2.start();
			}
		});

		JButton diskJbt = new JButton("磁盘管理");
		diskJbt.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				ui1.start();
			}
		});

		JButton procJbt = new JButton("进程管理");
		procJbt.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				ui4.start();
			}
		});

		JButton storageJbt = new JButton("内存管理");
		storageJbt.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				ui3.start();
			}
		});
		
		/*
		try{
			ui1.start();
			ui2.start();
			ui3.start();
			ui4.start();
		}catch(Exception error){
		
		}
		*/
		frame.setSize(500,300);
		frame.setLocationRelativeTo(null);
		frame.add(devJbt);
		frame.add(procJbt);
		frame.add(storageJbt);
		frame.add(diskJbt);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		t.setDaemon(true);
		t.start();
	}

	private Thread t=new Thread(new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int storageSize,power,runTime;
			
			while(true){
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					
				}
			//不能申请0
			storageSize=(int)(Math.random()*300)+1;
			
			power=5;
			
			//
			
			runTime=(int)(Math.random()*12)+1;
			
			int[] devNums={(int)(Math.random()*1),(int)(Math.random()*2),(int)(Math.random()*2)};
			
			boolean result=(Boolean)controller.Ram.getObject().createProc(storageSize,power,runTime,devNums).get("result");
			//System.out.println("尝试申请内存："+result);
			}
			}});
	
}
